#!/bin/bash
if [ -n "$(command -v tput)" ] && [ $(tput colors) -ge 8 ] && [ -z "${PROOT_DISTRO_FORCE_NO_COLORS-}" ]; then
	RST="$(tput sgr0)"
	RED="${RST}$(tput setaf 1)"
	BRED="${RST}$(tput bold)$(tput setaf 1)"
	GREEN="${RST}$(tput setaf 2)"
	YELLOW="${RST}$(tput setaf 3)"
	BYELLOW="${RST}$(tput bold)$(tput setaf 3)"
	BLUE="${RST}$(tput setaf 4)"
	CYAN="${RST}$(tput setaf 6)"
	BCYAN="${RST}$(tput bold)$(tput setaf 6)"
	ICYAN="${RST}$(tput sitm)$(tput setaf 6)"
else
	RED=""
	BRED=""
	GREEN=""
	YELLOW=""
	BYELLOW=""
	BLUE=""
	CYAN=""
	BCYAN=""
	ICYAN=""
	RST=""
fi

#############################################################################
#
# FUNCTION TO PRINT A MESSAGE TO CONSOLE
#
# Prints a given text string to stderr. Handles escape sequences.
msg() {
	echo -e "$@" >&2
}
function rand(){
    min=$1
    max=$(($2-$min+1))
    num=$(($RANDOM+1000000000)) #增加一个10位的数再求余
    echo $(($num%$max+$min))
}
rm_vncserver()
{
pt=$1
echo "kill vncserver:$pt"
vncserver -kill :$pt
rm -rf /tmp/.X$pt-lock
rm -rf /tmp/.X11-unix/X$pt
}

export USER=$(whoami)
HEIGHT=0
WIDTH=0
CHOICE_HEIGHT=5
BACKTITLE="分辨率选择"
TITLE="vnc服务启动选项"
MENU="输入序号(5秒后默认选择1):"
export PORT=2

if [ -z "$1" ];then
	PORT=2
elif [[ $1 -ge 0 ]] &&  [[ $1 -le 9 ]] 
then
	PORT=$1
	#echo current ${PORT}
fi



#sleep 10
OPTIONS=(0 "使用  shell 进入命令行"
         1 "使用  1440x720 分辨率(适用电脑远程)"
         2 "使用  1080x540 分辨率"
         3 "使用  1920x1080 分辨率"
         4 "使用  1920x920 分辨率"
         5 "使用  自定义分辨率及端口(用于多开)"
         6 "使用  540x1080 分辨率(适用手机竖屏远程)"
         7 "使用  640x1280 分辨率"
         8 "使用  720x1440 分辨率")

CHOICE=$(dialog --clear \
                --backtitle "$BACKTITLE" \
                --title "$TITLE" \
				--timeout 5 \
                --menu "$MENU" \
                $HEIGHT $WIDTH $CHOICE_HEIGHT \
                "${OPTIONS[@]}" \
                2>&1 >/dev/tty)

clear

if [ -z "${CHOICE}" ];then
			CHOICE=0
			echo CHOICE $CHOICE
fi
case $CHOICE in
        0)  exit
            ;;
        1)
			rm_vncserver ${PORT} 
            GEO="-geometry 1440x720" PORT=${PORT}  vnc
            ;;
        2)
			rm_vncserver ${PORT} 
            GEO="-geometry 1080x540" PORT=${PORT} vnc
            ;;
        3)
			rm_vncserver ${PORT} 
            GEO="-geometry 1920x1080" PORT=${PORT} vnc
            ;;
        4)
			rm_vncserver ${PORT} 
            GEO="-geometry 1920x920" PORT=${PORT} vnc
            ;;
        5)
            echo "使用自定义分辨率及端口"
            echo "请输入自定义分辨率 宽x高 例如 1920x1080，按回车后默认值为1920x920"
            read custom
	    if [ -z "${custom}" ];then
			custom=1920x920
	    fi
            
	    echo "请输入端口号 例如 1,按回车后默认值为3"
            read PORT
            if [ -z "${PORT}" ];then
                PORT=3
            fi
			rm_vncserver ${PORT}
            GEO="-geometry $custom" PORT=${PORT} vnc 
            ;;
			
		6)
			rm_vncserver ${PORT} 
            GEO="-geometry 540x1080" PORT=${PORT} vnc
            ;;
        7)
			rm_vncserver ${PORT} 
            GEO="-geometry 640x1280" PORT=${PORT} vnc
            ;;
        8)
			rm_vncserver ${PORT} 
            GEO="-geometry 720x1440" PORT=${PORT} vnc
            ;;
esac

#/usr/local/bin/noVNC/utils/launch.sh>/dev/null &
/usr/local/bin/noVNC/utils/launch.sh --listen "808${PORT}" --vnc "localhost:590${PORT}">/dev/null &

#sleep 1
clear
#local_ip=$(ip a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tail -n 1)
local_ip=$(ifconfig|grep inet|grep -v 127.0.0.1|grep -v inet4|awk '{print $2}'|tail -n 1|cut -d/ -f1)
local_ip=${local_ip%/*}

msg "控制方法1、通过访问页面进行控制，地址："

msg "${BLUE}http://${local_ip}:808${PORT}/vnc.html${RST}\n"
msg "使用${BLUE}同一内网${RST}另外一部手机${GREEN}微信${RST}扫描屏幕下方二维码："
qrencode -t UTF8 "http://${local_ip}:808${PORT}/vnc.html"
msg "\n默认密码:${RED}123456${RST}\n\n\n"
msg "控制方法2、使用vnc软件进行控制,地址："
msg "${BLUE}${local_ip}:590${PORT}${RST}"
msg "默认密码:${RED}123456${RST}\n"
msg "使用命令${BLUE}vncpasswd${RST}修改vnc连接默认密码\n\n"


#local_ipv6=$(ip a|grep inet|grep -v 127.0.0.1|grep -v inet4|grep inet6|awk '{print $2}'|tail -n 1|cut -d/ -f1)
#if [ ${#local_ipv6} -ge 20 ];then
#msg "使用另外一部手机${GREEN}微信${RST}扫描屏幕下方${BLUE}ipv6${RST}二维码"
#msg "http://[${local_ipv6}:808${PORT}]/vnc.html"
#qrencode -t UTF8 "http://[${local_ipv6}:808${PORT}]/vnc.html"
#fi 
msg "或者使用另外一部手机${GREEN}微信${RST}扫描屏幕下方${BLUE}公网服务器${RST}中转二维码："



#model="$(/data/data/com.sermux/files/usr/bin/getprop ro.product.brand)_$(/data/data/com.sermux/files/usr/bin/getprop ro.product.model)" 
#ait="$(cat ~/sysinstallTime)"
#if [[ $ait == "" ]];then 
#ait=`date '+%Y%m%d%H%M%S'`
#echo ${ait} > ~/sysinstallTime
#fi
#machine_name=${model}_${ait}

model="$(/data/data/com.sermux/files/usr/bin/getprop ro.product.brand)_$(/data/data/com.sermux/files/usr/bin/getprop ro.product.model)" 
if [[ -f ~/sysinstallTime ]]
then 
	ait="$(cat ~/sysinstallTime)"
else 
	ait=`date '+%Y%m%d%H%M%S'`
	echo ${ait} > ~/sysinstallTime
fi
machine_name=${model}_${ait}
echo ${machine_name}


rm -rf ~/frpc.ini
url_web=su1.ltd
rnd_port=$(rand 10000 50000) 
cat << EOF > ~/frpc.ini
[common]
server_addr = ${url_web}
server_port = 7000
token = 88888888
[${machine_name}]
type = tcp
local_ip = 127.0.0.1
local_port = 8082
remote_port = ${rnd_port}
EOF

qrencode -t UTF8 "http://${url_web}:${rnd_port}/vnc.html" 
#msg "http://${url_web}:${rnd_port}/vnc.html" 
pkill frpc
nohup ~/frpc -c ~/frpc.ini >/dev/null 2>&1 & 
echo "http://${url_web}:${rnd_port}/vnc.html" 

